package demo2;

public class Test2 {
    //将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
    public MySingeList.ListNode mergeTwoLists(MySingeList.ListNode list1, MySingeList.ListNode list2) {
        MySingeList.ListNode newHead = new MySingeList.ListNode(-1);//这里就需要将内部类ListNode编变成静态内部类，使得其不依赖于外部类对象，也就是不用进行外部类的实例化就可以实例化内部类
        MySingeList.ListNode tmpH = newHead;
        while(list1!=null&&list2!=null){
            if (list1.val< list2.val){
                tmpH.next = list1;
                tmpH = tmpH.next;
                list1 = list1.next;
            }else{
                tmpH.next = list2;
                tmpH = tmpH.next;
                list2 = list2.next;
            }
        }
        if (list1!=null){
            tmpH.next = list1;
        }
        if(list2!=null){
            tmpH.next = list2;
        }
        return newHead.next;
    }
    public static void main(String[] args) {
        Test2 test2 = new Test2();
        MySingeList mySingeList1 = new MySingeList();
        mySingeList1.addFirst(86);
        mySingeList1.addFirst(75);
        mySingeList1.addFirst(54);
        mySingeList1.addFirst(41);
        mySingeList1.addFirst(26);

        MySingeList mySingeList2 = new MySingeList();
        mySingeList2.addFirst(43);
        mySingeList2.addFirst(32);
        mySingeList2.addFirst(24);
        mySingeList2.addFirst(19);

        MySingeList.ListNode ret = test2.mergeTwoLists(mySingeList1.head,mySingeList2.head);
        mySingeList2.show(ret);

    }
    public static void main1(String[] args) {
        MySingeList mySingeList = new MySingeList();
        mySingeList.creatList();
        mySingeList.show();
        System.out.println("dyegfiurfyri");
        System.out.println(mySingeList.contains(12));
        System.out.println(mySingeList.contains(23));
        System.out.println(mySingeList.contains(34));
        System.out.println(mySingeList.contains(45));
        System.out.println(mySingeList.contains(56));
        System.out.println(mySingeList.contains(112));
        mySingeList.addFirst(99);
        mySingeList.addLast(101);
        mySingeList.show();
        mySingeList.addIndex(3,77);
        mySingeList.show();
        mySingeList.addIndex(0,88);
        mySingeList.show();
        mySingeList.addIndex(9,99);
        mySingeList.show();
//        mySingeList.remove(99);
//        mySingeList.show();
//        mySingeList.remove(110);
//        mySingeList.show();
        mySingeList.removeAllKey(99);
        mySingeList.show();
        mySingeList.removeAllKey(88);
        mySingeList.show();
        MySingeList.ListNode ret = mySingeList.reverseList();
        mySingeList.show(ret);
        MySingeList.ListNode ret1 = mySingeList.findKthToTail(4);
        System.out.println(ret1.val);
//        mySingeList.clear();
//        System.out.println("清空链表：");
//        mySingeList.show();


//        mySingeList.addIndex(10,77);
//        mySingeList.show();

    }
}
